<template>
  <div class="login-container">
    <div class="form-container">
      <h2>Haredot后台管理系统</h2>
      <form>
        <div class="form-group">
          <label for="username">用户名</label>
          <input type="text" id="username" v-model="username" />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="password" />
        </div>
        <div class="form-group remember">
          <Switch v-model="rememberMe">
            <template #open>
              <span>ON</span>
            </template>
            <template #close>
              <span>OFF</span>
            </template>
          </Switch><label>记住我</label>
        </div>
        <button type="submit" @click.prevent="handleSubmit">登录</button>
      </form>
    </div>
  </div>
</template>

<script>

import Token from '../api/token.js'

export default {
  data() {
    return {
      username: '',
      password: '',
      rememberMe: true,
    };
  },
  methods: {
    handleSubmit() {
      // 调用 /api/token 接口，传入 用户名和密码获取令牌
      this.$api.login(this.username, this.password).then(({data:ret})=> {
         if (ret.status === "10000") {
             // 获取 访问令牌 和 刷新令牌
            let accessToken = ret.accessToken ;
            let refreshToken = ret.refreshToken ;

            Token.saveToken(accessToken, refreshToken, this.rememberMe);
            // 跳转到首页路径
            this.$router.push({name: "Home"})
         }else{
            this.$Message.error({background: true, content: ret.message});
         }
      })
    },
  },
};
</script>

<style scoped>
.login-container {
  background-image: linear-gradient(to bottom right, #88bdbc, #ffe5b4);
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  background-color: rgba(255, 255, 255, 0.9);
  padding: 50px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
}

.remember {
  text-align: right;
}

h2 {
  margin-bottom: 20px;
  text-align: center;
  color: #5c5c5c;
}

form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

label {
  display: inline-block;
  margin-bottom: 5px;
  color: #5c5c5c;
  width: 80px;
  padding:0 15px;
  text-align: right;
  box-sizing: border-box;
}

label[for=rememberMe] {
  padding: 0 15px 0 5px;
  text-align: left;
  width: auto;
}

input[type='text'],
input[type='password'] {
  width: 460px;
  height: 40px;
  padding: 5px;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  background-color: #f5f5f5;
  color: #5c5c5c;
}

input[type='text']:focus,
input[type='password']:focus {
  outline: none;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

input[type='checkbox'] {
  display: inline-block;
  margin-right: 10px;
  transform: translateY(2px);
}

button[type='submit'] {
  height: 40px;
  border: none;
  border-radius: 5px;
  background-color: #5c5c5c;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}

button[type='submit']:hover {
  background-color: #444;
}


</style>
